let updateItemApp=new Vue({
    el:"#updateItemApp",
    data:{
        item:{
            itemsName:"",
            catId:"",
            itemsSynopsis:"",
            unitPrice:"",
            specification:"",
            stock:""
        },
        itemId:"",
        categories:{},
        catId:""
    },
    methods:{
        loadItem:function () {
            let itemId = location.search;
            if (!itemId) {
                alert("请输入问题id")
                return;
            }
            itemId=itemId.substring(1);
            axios({
                url: "/backItems/get/" + itemId,
                method: "get"
            }).then(function (response) {
                updateItemApp.item = response.data
                let item=updateItemApp.item
                updateItemApp.itemId=itemId;
                item.catId=response.data.catId
                for (let i = 0; i < updateItemApp.categories.length; i++) {
                    if (item.catId ===updateItemApp.categories[i].id){
                        $("#dropdownMenu1").text(updateItemApp.categories[i].catName)
                    }
                }
                if (item.itemsStatus===1){
                    $("#dropdownMenu2").text("在售")
                }else{
                    $("#dropdownMenu2").text("下架")
                }
                $('#summernote').summernote("code",item.itemsContent);
            })
        },
        loadCate:function () {
            axios({
                url:"/categories",
                method:"get"
            }).then(function (response) {
                updateItemApp.categories=response.data;
            })
        },
        select:function (catId,catName) {
            $("#dropdownMenu1").text(catName);
            updateItemApp.catId=catId;
        },
        updateItem:function () {
            let files=document.getElementById("imageFile").files;
            let url;
            if (files.length > 0){
                let imageUrl=$("#image").attr("src")
                for (let i = 0; i < 3; i++) {
                    imageUrl=imageUrl.slice(imageUrl.indexOf("/")+1);
                }
                url=imageUrl.substring(0);
            }else {
                let imageUrl=$("#image").attr("src")
                url=imageUrl.substring(3);
            }
            let content = $('#summernote').val();
            let statusName=$("#dropdownMenu2").text();
            let status;
            if (statusName ==="在售"){
                status=1;
            }else {
                status=2;
            }

            let form=new FormData;
            form.append("itemsName",this.item.itemsName);
            form.append("catId",this.catId);
            form.append("itemsImage",url);
            form.append("itemsSynopsis",this.item.itemsSynopsis);
            form.append("unitPrice",this.item.unitPrice);
            form.append("specification",this.item.specification);
            form.append("stock",this.item.stock);
            form.append("itemsStatus",status);
            form.append("itemsContent",content);
            console.log(form)
            axios({
                url:"/backItems/update/"+updateItemApp.itemId,
                method:"post",
                data:form
            }).then(function (response) {
                if (response.data === "ok"){
                    alert("商品修改成功，确认跳转商品列表")
                    location.href="/back/index.html"
                }else {
                    alert(response.data)
                }
            })
        },
    },
    created:function () {
        this.loadCate()
        this.loadItem()
    }
})